﻿using IntoStationSystem.Db;
using IntoStationSystem.Enums;
using System.Collections.Generic;
using System.Linq;

namespace IntoStationSystem.Global
{
    public class MsgHelper
    {
        public static Dictionary<int, string> AlarmDic = new Dictionary<int, string>();

        public static Dictionary<int, string> StepTipDic = new Dictionary<int, string>();

        public static Dictionary<int, string> TipInfoDic = new Dictionary<int, string>();

        public static Dictionary<int, string> WarningDic = new Dictionary<int, string>();
        public static Dictionary<int, string> SysStatusDic = new Dictionary<int, string>();

        public static void LoadMsgCfg()
        {
            using (ConfigDbContext db = new ConfigDbContext())
            {
                var msgs = db.Msgs.ToList();
                if (msgs != null && msgs.Count > 0)
                {
                    foreach (var msg in msgs)
                    {
                        switch (msg.MsgType)
                        {
                            case MsgType.Tip:
                                TipInfoDic.Add(msg.Index, msg.Content);
                                break;
                            case MsgType.Alarm:
                                AlarmDic.Add(msg.Index, msg.Content);
                                break;
                            case MsgType.Warning:
                                WarningDic.Add(msg.Index, msg.Content);
                                break;
                            case MsgType.Step:
                                StepTipDic.Add(msg.Index, msg.Content);
                                break;
                            case MsgType.SysStatus:
                                SysStatusDic.Add(msg.Index, msg.Content);
                                break;
                        }
                    }
                }
                if (TipInfoDic.Count == 0) DefaultMsg(TipInfoDic, MsgType.Tip, db);
                if (AlarmDic.Count == 0) DefaultMsg(AlarmDic, MsgType.Alarm, db);
                if (WarningDic.Count == 0) DefaultMsg(WarningDic, MsgType.Warning, db);
                if (StepTipDic.Count == 0) DefaultMsg(StepTipDic, MsgType.Step, db);
                if (SysStatusDic.Count == 0) DefaultMsg(SysStatusDic, MsgType.SysStatus, db);
                db.SaveChanges();
            }

        }

        private static void DefaultMsg(Dictionary<int, string> dic, MsgType type, ConfigDbContext db)
        {
            for (int i = 0; i < 32; i++)
            {
                dic.Add(i, $"default{i + 1}");
                db.Msgs.Add(new Entity.Msg()
                {
                    MsgType = type,
                    Content = $"default{i + 1}",
                    Index = i + 1,
                });
            }
        }
    }
}
